package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author lfh
 * @version 1.0
 * @date 2021/4/11 11:15
 */
//@CrossOrigin
@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("admin/product")
public class BaseManageController {
    //控制器：注入服务层
    @Autowired
    private ManageService manageService;


    /**
     * 查询所有的一级分类数据
     *
     * @return
     */
    //http://api.gmall.com/admin/product/getCategory1
    @ApiOperation(value = "查询所有的一级分类数据")
    @GetMapping("getCategory1")
    public Result getCategory1() {
        //  返回数据
        return Result.ok(manageService.getBaseCategory1());
    }

    /**
     * 根据一级分类Id 查询二级分类数据
     *
     * @param category1Id
     * @return
     */
    //http://api.gmall.com/admin/product/getCategory2/{category1Id}
    @ApiOperation(value = "根据一级分类Id 查询二级分类数据")
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id) {
        //  返回数据
        return Result.ok(manageService.getBaseCategory2(category1Id));
    }

    /**
     * 根据二级分类Id 查询三级分类数据
     *
     * @param category2Id
     * @return
     */
    //http://api.gmall.com/admin/product/getCategory3/{category2Id}
    @ApiOperation(value = "根据二级分类Id 查询三级分类数据")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id) {
        //  返回数据
        return Result.ok(manageService.getBaseCategory3(category2Id));
    }

    /**
     * 根据分类Id 查询平台属性集合
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    //  http://api.gmall.com/admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @ApiOperation(value = "根据分类Id 查询平台属性集合")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id) {
        //  返回数据
        return Result.ok(manageService.getBaseAttrInfoList(category1Id, category2Id, category3Id));
    }

    /**
     *  保存平台属性
     * @param baseAttrInfo
     * @return
     */
    /*
        http://api.gmall.com/admin/product/saveAttrInfo
        后台控制器需要 接收到前端传递的参数、 数据！
        baseAttrInfo 这个对象的属性与传递的数据格式类似！ 需要将Json --> JavaObject {@RequestBody}
     */
    @ApiOperation(value = "根据分类Id 查询平台属性集合")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        // 前台数据都被封装到该对象中baseAttrInfo
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @ApiOperation(value = "根据平台属性Id 获取平台属性值集合")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //select * from base_attr_value where attr_id = attrId
        //select * from base_attr_info where id = attrId; if(baseAttrInfo != null )
        //先判断一下当前是否存在该属性：如果有属性，则调用属性对象的属性值集合

        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);
        if (baseAttrInfo != null){
            return Result.ok(baseAttrInfo.getAttrValueList());
        }
        //List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);

        //返回数据
        //return Result.ok(baseAttrValueList);
        return Result.ok();
    }

}
